package com.lau.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lau.mapper.TSysUserMapper;
import com.lau.pojo.TSysUser;
import com.lau.service.TSysUserService;
import com.lau.utils.DateUtils;
import com.lau.utils.PageUtils;
import com.lau.vo.PagingData;
import com.lau.vo.PagingParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 系统_用户认证 服务实现类
 * </p>
 *
 * @author Lau
 * @since 2022-12-07
 */
@Service
@Slf4j
public class TSysUserServiceImpl extends ServiceImpl<TSysUserMapper, TSysUser> implements TSysUserService {

    @Autowired
    TSysUserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<TSysUser> queryUserListByLoginName(String longinName) {
        QueryWrapper<TSysUser> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<TSysUser> like = wrapper.lambda().like(TSysUser::getLoginName, longinName);
        return userMapper.selectList(like);
    }

    @Override
    @Cacheable(value = "minutes_1")
    public List<TSysUser> queryUserListByName(String name) {
        return userMapper.queryUserListByName(name);
    }

    @Override
    public TSysUser queryUserListById(String id) {
        return userMapper.selectById(id);
    }

    @Override
    @Transactional
    public void test()  {
        deleteUserById("10000");
        TSysUser user = new TSysUser();
        user.setUserId("30000");
        user.setLoginName("test");
        saveUser(user);
    }

    /**
     * 模拟插入订单
     */
    @Override
    public void testOrderExpiration() {
        log.info("模拟插入订单，插入时间{}", DateUtils.getTime());
        //将结果放入缓存，设置一天有效期
        redisTemplate.opsForValue().set("test","我是测试数据",1, TimeUnit.MINUTES);
    }

    public void saveUser(TSysUser user)  {
        userMapper.insert(user);
    }

    public void deleteUserById(String id) {
        userMapper.deleteById(id);
    }

    @Override
    @Async("async")
    public void testAsync(int i) throws InterruptedException {
        log.info("线程" + Thread.currentThread().getName() + " 执行异步任务：" + i);
        Thread.sleep(5000);
    }

    @Override
    @Cacheable(value = "minutes_1")
    public List<TSysUser> queryUserListByCondition(String userName,String loginName) {
        return userMapper.queryUserListByCondition(userName,loginName);
    }

    @Override
    public PagingData<TSysUser> pageUserListByName(String name, PagingParams pageParams) {
        QueryWrapper<TSysUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(TSysUser::getUserName, name);
        IPage<TSysUser> page = userMapper.selectPage(PageUtils.getPage(pageParams), wrapper);
        return PageUtils.getPageData(page,pageParams, TSysUser.class);
    }

    @Override
    public PagingData<TSysUser> pageUserListByCondition(String userName, String loginName, PagingParams pageParams) {
        IPage<Object> page = PageUtils.getPage(pageParams);
        List<TSysUser> tSysUsers = userMapper.pageUserListByCondition(userName, loginName,
                page);
        return PageUtils.getPageData(page,pageParams, tSysUsers);
    }

}
